<template>
  <div :style="{'height':windowsHeight-65+'px',...userStore.themeColor}">
    <CX_List
      ref="cxlist"
      url="Collect/GetShareCollect"
      method="post"
      :height="windowsHeight-70"
      @loadOk="loadOk"
      :keyword="keyword">
      <template v-slot:default="{item}">
        <span title="加入的我收藏" class="settings mousecursor">
          <icon-plus-circle size="18" @click="openadd(item)"/>
        </span>
      </template>
    </CX_List>
    <a-modal :mask-closable="false" :width="350" :visible="show" @cancel="show=false;" @ok="adduserfiled" draggable>
      <template #title>请选择要添加到的文件夹位置</template>
      <a-select :default-active-first-option="true" v-model="addfiled" placeholder="请选择文件夹">
        <a-option v-for="item of userFolderAll" :value="item" :label="item" />
      </a-select>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import UseWindowsInfo from "../../hooks/UseWindowsInfo";
import {useUserStore} from "../../store/user";
import {onMounted, onUnmounted, reactive, ref} from "vue";
import Cx_Bus from "../../common/Utils/MittUtils";
import CX_List from "../../components/CX_List/CX_List.vue";
import {CX_ListType} from "../../common/Type/CX_ListType";
import {CollectType} from "../../common/Type/CollectType";
import {RequestUtils} from "../../common/Utils/RequestUtils";
import CXMessageUtils from "../../common/Utils/CXMessageUtils";

const {windowsHeight} = UseWindowsInfo()
const userStore = useUserStore()
const cxlist = ref()
/**
 * 搜索条件
 */
const keyword = ref("")
// 加入我的收藏弹窗
const show = ref(false);
const addfiled = ref("");
let methods:CX_ListType = reactive({} as CX_ListType)
// 用户的所有文件夹名称
const userFolderAll = ref<string[]>([])
// 要添加的收藏uuid
const addfileuuid = ref("")

/**
 * 组件加载完成
 * @param m
 */
const loadOk = (m):void=>{
  methods = m;
}
/**
 * 打开添加到我的收藏弹窗
 */
const openadd = (item:CollectType):void=>{
  GetFolderNameAll()
  show.value = true
  addfileuuid.value = item.buuid
}

/**
 * 获取用户所有的文件夹名称
 * @constructor
 */
const GetFolderNameAll = ()=>{
  return new Promise((resolve,reject)=>{
    RequestUtils({
      url: `Folder/GetFolderNameAll`,
      method: "get"
    }).then(data => {
      userFolderAll.value = data as any
      resolve(data as any)
    }).catch(err=>{
      reject(err)
    })
  })
}

// 添加到我的收藏
const adduserfiled=():void=>{
  if (addfileuuid){
    RequestUtils({
      url:`Collect/AddCopyUserCollect?uuid=${addfileuuid.value}&filename=${addfiled.value}`,
      method:'get'
    }).then(()=>{
      CXMessageUtils.CX_Success("添加成功")
      show.value = false
      addfileuuid.value = ""
      addfiled.value = ""
    })
  }
  else {
    CXMessageUtils.CX_Error("请选择文件夹")
  }
}

onMounted(() => {
  // 开启监听
  Cx_Bus.on("Tool-Search", (keywordvalue) => {
    keyword.value = keywordvalue
    methods.LoadData()
  })
  Cx_Bus.on("Tool-AddData",()=>{
    methods.LoadData()
  })
  Cx_Bus.on("CX-deleteWebPage",()=>{
    methods.LoadData()
  })
})
onUnmounted(() => {
  // 关闭监听
  Cx_Bus.off("Tool-Search")
  Cx_Bus.off("Tool-AddData")
  Cx_Bus.off("CX-deleteWebPage")
})
</script>

<style scoped>
.settings {
  margin: 0 4px;
}
</style>
